package datastructure.sort_algorithm;

import java.util.Arrays;

/**
 * 选择排序  O(n^2)
 * @author lbw
 */
public class SelectSort {

    public static void main(String[] args) {
        int[] arr = {3, 9, -1, 10, -2};
        selectSorting(arr);
        System.out.println(Arrays.toString(arr));
    }

    public static void selectSorting(int[] arr){
        int minNum;
        int index = 0;
        for (int i = 0; i < arr.length; ++i){
            minNum = arr[i];
            for (int j = i+1; j < arr.length; ++j){
                //说明min不是真的最小值
                if (minNum > arr[j]){
                    //重置min
                    minNum = arr[j];
                    //重置minIndex
                    index = j;
                }
            }
            //判断是否需要交换,如果最小值的下标就是i本身，则不需要交换
            if (index != i){
                arr[index] = arr[i];
                arr[i] = minNum;
            }
        }
    }
}
